﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASBCC.Database.EntityFramework;
using BEGDC.CC.BL.Workflow;
using CCEntity = ASBCC.Database.EntityFramework.CCEntities;

namespace ASBCC.Business
{
    /// <summary>
    /// 用来处理WORKSPACE, CCS, CIS, ACIS, EZVISIT, ADMIN模块的主菜单和侧栏菜单的显示
    /// </summary>
    public static class MenuHelper
    {
        /// <summary>
        /// 获取当前页面User的CSPrincipal信息
        /// </summary>
        /// <returns></returns>
        private static CSPrincipal GetCCPrincipal()
        {
            var principal = HttpContext.Current.User as CSPrincipal;

            if (principal == null)
                throw new ApplicationException("CCPrincipal is null");

            return principal;
        }

        /// <summary>
        /// 根据当前登录用户的角色来获取主菜单
        /// </summary>
        public static IEnumerable GetMainMenu()
        {
            var user = GetCCPrincipal();

            using (var dc = new CCEntity())
            {
                string role2s = "";
                foreach (var role in user.Role2s)
                {
                    role2s += "'" + role + "',";
                }
                role2s = role2s.Substring(0, role2s.Length - 1);
                var menus = dc.sp_GetMenuH1New(role2s);
                var source = from menu in menus
                             select new
                             {
                                 menu.MenuID,
                                 menu.Name,
                                 menu.Description,
                                 Agreement = GetAgreement(menu.Name),
                             };
                return source.ToArray();
            }
        }

        private static string GetAgreement(string menu)
        {
            //if(menu == "CCS")
            //    return "CCAgreement.html";
            if (menu == "CIS")
                return "CISAgreement.aspx";
            return "";

            //try
            //{
            //    SystemModule sys = (SystemModule)Enum.Parse(typeof(SystemModule), belongsTo);
            //    if (sys == SystemModule.CCD || sys == SystemModule.CCO)
            //        return "CCAgreement.html";
            //    else if (sys == SystemModule.CID || sys == SystemModule.CIO)
            //        return "CISAgreement.html";
            //    else
            //        return String.Empty;
            //}
            //catch
            //{
            //    return String.Empty;
            //}
        }

        /// <summary>
        /// 根据当前登录用户的角色来获取侧栏菜单
        /// 
        /// TODO: Performance warning -> This method takes more than 5 seconds!!!
        /// </summary>
        public static IEnumerable GetSidebarMenu(int sysFID)
        {
            if (sysFID == 888)
            {
                List<Menu> menu = new List<Menu>();
                Menu m1 = new Menu();
                m1.Name = "Customer Info";
                m1.Description = "客户信息";
                menu.Add(m1);
                Menu m2 = new Menu();
                m2.Name = "Seach";
                m2.Description = "信息查询";
                menu.Add(m2);
                Menu m3 = new Menu();
                m3.Name = "Shared Info";
                m3.Description = "共享信息";
                menu.Add(m3);
                Menu m4 = new Menu();
                m4.Name = "Administration";
                m4.Description = "Admin";
                menu.Add(m4);
                var source1 = from menu1 in menu
                              select new
                              {
                                  H2 = menu1.Name,
                                  H2Desc = menu1.Description,
                                  H3List = GetStaticMenu3(menu1.Name)
                              };
                return source1.ToArray();
            }
            if (sysFID == 889)
            {
                List<Menu> menu = new List<Menu>();
                Menu m1 = new Menu();
                m1.Name = "Customer Infos";
                m1.Description = "客户信息";
                menu.Add(m1);
                Menu m2 = new Menu();
                m2.Name = "Seach";
                m2.Description = "信息查询";
                menu.Add(m2);
                Menu m3 = new Menu();
                m3.Name = "Shared Info";
                m3.Description = "共享信息";
                menu.Add(m3);
                Menu m4 = new Menu();
                m4.Name = "ADministration";
                m4.Description = "Admin";
                menu.Add(m4);
                var source1 = from menu1 in menu
                              select new
                              {
                                  H2 = menu1.Name,
                                  H2Desc = menu1.Description,
                                  H3List = GetStaticMenu3(menu1.Name)
                              };
                return source1.ToArray();
            }

            if (sysFID == 0)
                return null;

            var user = GetCCPrincipal();

            using (var dc = new CCEntity())
            {
                string role2s = "";
                foreach (var role in user.Role2s)
                {
                    role2s += "'" + role + "',";
                }

                role2s = role2s.Substring(0, role2s.Length - 1);
                var menus = dc.sp_GetMenuH2New(sysFID, role2s);
                var source = from menu in menus
                             orderby menu.Sort
                             select new
                             {
                                 H2 = menu.Name,
                                 H2Desc = menu.Description,
                                 H3List = GetH3List(dc, menu.MenuID, role2s, menu.Name),
                             };
                if (dc.Menus.First(m => m.MenuID == sysFID).Name == "CCS")
                {
                    var m = new
                    {
                        H2 = "Search",
                        H2Desc = "搜索",
                        H3List = GetMySearchH3List()
                    };
                    var ss = source.ToList();
                    int insertIndex = 0;
                    foreach (var m2 in ss)
                    {
                        if (m2.H2.StartsWith("OCV")
                            || m2.H2.StartsWith("OCR")
                            || m2.H2.StartsWith("CR")
                            || m2.H2.StartsWith("EV"))
                        {
                            insertIndex++;
                        }
                    }
                    ss.Insert(insertIndex, m);
                    return ss.ToArray();
                }
                return source.ToArray();
            }
        }

        private static IEnumerable GetH3List(CCEntity dc, int parentSysFID, string roles, string menuName)
        {
            var menus = dc.sp_GetMenuH3New(parentSysFID, roles);
            var source = from menu in menus
                         select new
                         {
                             H3 = menu.Name,
                             H3Desc = menu.Description,
                             Url = menu.Target,
                         };
            return source.ToArray();
        }

        private static IEnumerable GetMySearchH3List()
        {
            BEGDC.CC.BL.AdvancedSearch.IAdvancedSearch iquery = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.SearchService] as BEGDC.CC.BL.AdvancedSearch.IAdvancedSearch;
            System.Data.DataTable dtSearchs = iquery.QueryMySearchList(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id);

            foreach (System.Data.DataRow drSearch in dtSearchs.Rows)
            {
                var s = new
                {
                    H3 = drSearch["SearchName"].ToString(),
                    H3Desc = drSearch["Description"].ToString(),
                    Url = string.Format("view/MySearch.aspx?searchid={0}", drSearch["SearchID"].ToString())
                };
                yield return s;
            }
        }

        private static IEnumerable GetStaticMenu3(string menu2)
        {
            List<Menu> menu = new List<Menu>();
            if (menu2 == "Customer Info")
            {
                Menu m2 = new Menu();
                m2.Name = "Customer List";
                m2.Description = "客户信息";
                m2.Target = "CIS2/Static/CustomerInfo.html";
                menu.Add(m2);
                Menu m1 = new Menu();
                m1.Name = "Create";
                m1.Description = "创建客户";
                m1.Target = "CIS2/Static/CreateCustomer.html";
                menu.Add(m1);
                Menu m3 = new Menu();
                m3.Name = "Draft";
                m3.Description = "草稿箱";
                m3.Target = "CIS2/Static/CustomerDraft.html";
                menu.Add(m3);
            }
            if (menu2 == "Customer Infos")
            {
                Menu m2 = new Menu();
                m2.Name = "Customer List";
                m2.Description = "客户信息";
                m2.Target = "CIS2/Static/CustomerInfoOver.html";
                menu.Add(m2);
                Menu m1 = new Menu();
                m1.Name = "Create";
                m1.Description = "创建客户";
                m1.Target = "CIS2/Static/CreateCustomerOver.html";
                menu.Add(m1);
                Menu m3 = new Menu();
                m3.Name = "Draft";
                m3.Description = "草稿箱";
                m3.Target = "CIS2/Static/CustomerDraftOver.html";
                menu.Add(m3);
            }
            if (menu2 == "Seach")
            {
                Menu m1 = new Menu();
                m1.Name = "Company Seatch";
                m1.Description = "公司查询";
                m1.Target = "#";
                menu.Add(m1);
                Menu m2 = new Menu();
                m2.Name = "Customer Search";
                m2.Description = "客户查询";
                m2.Target = "#";
                menu.Add(m2);
            }
            if (menu2 == "Shared Info")
            {
                Menu m1 = new Menu();
                m1.Name = "Shared Info";
                m1.Description = "共享信息";
                m1.Target = "#";
                menu.Add(m1);
            }
            if (menu2 == "Administration")
            {
                Menu m1 = new Menu();
                m1.Name = "Company Info";
                m1.Description = "公司信息";
                m1.Target = "CIS2/Static/CompanyInfo.html";
                menu.Add(m1);
                Menu m2 = new Menu();
                m2.Name = "Province Management";
                m2.Description = "省/直辖市管理";
                m2.Target = "#";
                menu.Add(m2);
                Menu m3 = new Menu();
                m3.Name = "CAT Managem";
                m3.Description = "大客户管理";
                m3.Target = "#";
                menu.Add(m3);
                Menu m4 = new Menu();
                m4.Name = "Cluster Managem";
                m4.Description = "地区管理";
                m4.Target = "#";
                menu.Add(m4);
                Menu m5 = new Menu();
                m5.Name = "Department Managem";
                m5.Description = "部门管理";
                m5.Target = "#";
                menu.Add(m5);
                Menu m6 = new Menu();
                m6.Name = "Customer Statistics";
                m6.Description = "客户统计";
                m6.Target = "#";
                menu.Add(m6);
                Menu m7 = new Menu();
                m7.Name = "Role Assignment";
                m7.Description = "国内角色分配";
                m7.Target = "#";
                menu.Add(m7);
                Menu m8 = new Menu();
                m8.Name = "Operation Log";
                m8.Description = "操作日志";
                m8.Target = "#";
                menu.Add(m8);
            }
            if (menu2 == "ADministration")
            {
                Menu m1 = new Menu();
                m1.Name = "Company Info";
                m1.Description = "公司信息";
                m1.Target = "CIS2/Static/CompanyInfoOver.html";
                menu.Add(m1);
                Menu m2 = new Menu();
                m2.Name = "Province Management";
                m2.Description = "省/直辖市管理";
                m2.Target = "#";
                menu.Add(m2);
                Menu m3 = new Menu();
                m3.Name = "CAT Managem";
                m3.Description = "大客户管理";
                m3.Target = "#";
                menu.Add(m3);
                Menu m4 = new Menu();
                m4.Name = "Cluster Managem";
                m4.Description = "地区管理";
                m4.Target = "#";
                menu.Add(m4);
                Menu m5 = new Menu();
                m5.Name = "Department Managem";
                m5.Description = "部门管理";
                m5.Target = "#";
                menu.Add(m5);
                Menu m6 = new Menu();
                m6.Name = "Customer Statistics";
                m6.Description = "客户统计";
                m6.Target = "#";
                menu.Add(m6);
                Menu m7 = new Menu();
                m7.Name = "Role Assignment";
                m7.Description = "国内角色分配";
                m7.Target = "#";
                menu.Add(m7);
                Menu m8 = new Menu();
                m8.Name = "Operation Log";
                m8.Description = "操作日志";
                m8.Target = "#";
                menu.Add(m8);
            }
            var source = from menu1 in menu
                         select new
                         {
                             H3 = menu1.Name,
                             H3Desc = menu1.Description,
                             Url = menu1.Target
                         };
            return source.ToArray();
        }
    }
}
